﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

using AppDialogBox;

namespace PasswordControl
{
    public partial class ControlProgram : Form
    {
    /* start of class */

        /*
        * variables 
        */
        public string ProfileLabel { get; set; }
        //if data was changed since the last save -> true
        public bool ListChanged { get; set; }
        //if profile default data changed
        public bool DefaultChanged { get; set; }
        //the owner that is now selected in the owners listbox
        private string m_currentOwner = string.Empty;
        //current password 
        public string CurrentPassword { get; set; }

        /*
        * methods 
        */
        //constructor
        public ControlProgram()
        {
            InitializeComponent();
        }
        //load form
        private void ControlProgram_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;

            //check if profiles folder exists, if not, create it
            if (!Directory.Exists(PROFILES_PATH)) Directory.CreateDirectory(PROFILES_PATH);

            //Init ProfileLabel
            this.ProfileLabel = DEFAULT_PROFILE_NAME;

            //Init profiles list
            ProfileData.LoadProfileData();

            //Make tool tips
            MakeToolTips();
            
            if (LoadProfile_Form.CheckSavedProfiles())
            {
                LoadProfile_Form loadform = new LoadProfile_Form(this);
                loadform.ShowDialog();
            }
            else
            {
                this.InitLists();
                this.WindowState = FormWindowState.Normal;
            }

            //for now, the checkbox of the find password ( contains ) is not visible.
            // I want it to be always checked
            this.CheckBoxContain.Checked = true;

            //give focus to the form
            this.Focus();
        }
        
        // finish button
        private void Finish_Click(object sender, EventArgs e)
        {
            this.MenuClose_Click(sender, e);
        }

        //add owner button
        private void AddOwner_Click(object sender, EventArgs e)
        {
            string owner, username, password, fieldname, fielddata, group;
            owner = this.addowner.Text; username = this.addusername.Text; password = this.addpassword.Text;
            fieldname = this.addfieldname.Text; fielddata = this.addfielddata.Text;
            group = this.combogroup.Text;
            if (group.Length == 0) group = GROUP_GENERAL;

            if ( owner.Length == 0)
            {
                AppDialogWrapper.PromptBox("Name must be given",MESSAGE_ERROR);
                return;
            }
            if (Owners.FindOwner(owner) != null)
            {
                AppDialogWrapper.PromptBox("Name already exists", MESSAGE_ERROR);
                return;
            }
            if (group == GROUP_DELETED || group == GROUP_ALL)
            {
                AppDialogWrapper.PromptBox("You can't name a group in the following names: 'All Groups' , 'Deleted'",MESSAGE_ERROR);
                return;
            }

            //add the owner to the list 
            Owners.AddOwner(owner, username, password , fieldname , fielddata , group);
            //add group if needed
            if (!this.combogroup.Items.Contains(group))
            {
                this.combogroup.Items.Add(group);
                this.ListGroups.Items.Insert(this.ListGroups.Items.Count - 1,group);  
            }

            this.addowner.Text = ""; this.addpassword.Text = ""; this.addusername.Text = ""; this.addfieldname.Text = ""; this.addfielddata.Text = ""; 

            this.ListGroups_SelectedIndexChanged(sender, e);
            this.ListChanged = true;
            this.labelSaved.Visible = false;
        }
        //clear form button
        private void buttonClear_Click(object sender, EventArgs e)
        {
            //clear all textboxs in 'Add Password' tab
            addowner.Text = "";
            addusername.Text = "";
            addpassword.Text = "";
            addfieldname.Text = "";
            addfielddata.Text = "";
            combogroup.SelectedIndex = 0;
        }
        //add group button
        private void buttonAddGroup_Click(object sender, EventArgs e)
        {
            EditNewGroup_Click(new object(), new EventArgs());
        }

        //delete an owner - double click owner list
        private void ListOwners_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.ListOwners.IndexFromPoint(e.X, e.Y) < 0) return;
            EditDeleteOwner_Click(sender, e);
        }
        //delete a group - double click group list
        private void ListGroups_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            EditNewGroup_Click(sender, e);
        }


        //group list changed
        public void ListGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ListGroups.Items.Count == 0)
                return;

            string group = this.ListGroups.SelectedItem.ToString();

            this.ListOwners.Items.Clear();

            if (group == GROUP_ALL) //all groups allways at index 0
            {
                foreach (Owners temp in Owners.GetOwners())
                {
                    if (temp.Group != GROUP_DELETED)
                        this.ListOwners.Items.Add(temp.Owner);
                }
            }
            else
            {
                foreach (Owners temp in Owners.GetOwners())
                {
                    if (temp.Group == group) this.ListOwners.Items.Add(temp.Owner);
                }
            }

            if (this.ListOwners.Items.Count > 0)
            {
                this.ListOwners.SelectedItem = this.ListOwners.Items[0];
            }
            else
            {
                this.savedUsername = "  ";
                this.savedPassword = "  ";
                this.textusername.Text = this.savedUsername;
                this.textpassword.Text = this.savedPassword;

                this.textfield.Visible = false;
                this.label_field.Visible = false;
            }
        }
        //owner list changed
        public void ListOwners_SelectedIndexChanged(object sender, EventArgs e)
        {
            string owner = this.ListOwners.Text;
            Owners owner_class = Owners.FindOwner(owner);

            this.ButtonEdit.Visible = false;

            /*
            if (this.current_owner.Length > 0 && this.CheckBoxDisplay.Checked)
            {
                Control_Program.Owners prev_owner = Owners.Find_Owner(this.current_owner);

                if (this.textusername.Text != prev_owner.username || this.textpassword.Text != prev_owner.password || this.textfield.Text != prev_owner.fielddata)
                {
                    this.ButtonEdit_Click(sender, e);
                }
            }
            */

            this.m_currentOwner = this.ListOwners.Text;

            if (owner.Length == 0 || owner_class == null || !this.CheckBoxDisplay.Checked)
            {
                this.savedUsername = "  ";
                this.savedPassword = "  ";

                this.textfield.Visible = false;
                this.savedFieldata = "";
                this.label_field.Visible = false;
                this.label_field.Text = "Optional Field";
            }
            else
            {
                this.savedUsername = owner_class.Username;
                this.savedPassword = owner_class.Password;
                if (owner_class.Fielddata.Length > 0)
                {
                    // I have data in the third field , do what is needed man!!
                    this.savedFieldata = owner_class.Fielddata;
                    this.label_field.Text = owner_class.Fieldname;

                    this.textfield.Visible = true;
                    this.label_field.Visible = true;
                }
                else
                {
                    this.textfield.Visible = false;
                    this.savedFieldata = "";
                    this.label_field.Visible = false;
                    this.label_field.Text = "Optional Field";
                }
            }

            this.textusername.Text = this.savedUsername;
            this.textpassword.Text = this.savedPassword;
            this.textfield.Text = this.savedFieldata;
        }
        //combobox groups changed
        public void combogroup_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }
        // selected tab changed
        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.tabControl.SelectedIndex;
            bool visible = false;

            if (index == 0)
                visible = true;

            MenuEdit.DropDownItems["EditDeleteGroup"].Visible = visible;
            MenuEdit.DropDownItems["EditDeleteOwner"].Visible = visible;
        }

        //mouse down list owner
        private void ListOwners_MouseDown(object sender, MouseEventArgs e)
        {
            bool doDragDrop = (e.Button == MouseButtons.Left && e.Clicks == 1);
            if (!doDragDrop) return;

            if (this.ListOwners.Items.Count == 0)
                return;

            int index = this.ListOwners.IndexFromPoint(e.X, e.Y);
            if (index >= 0)
            {
                this.ListOwners_SelectedIndexChanged(sender, e);
                string owner = this.ListOwners.Items[index].ToString();
                DragDropEffects dde = DoDragDrop(owner, DragDropEffects.All);
            }
        }
        //drag over list group
        private void ListGroups_DragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
                e.Effect = DragDropEffects.All;
        }
        //drag drop list group
        private void ListGroups_DragDrop(object sender, DragEventArgs e)
        {
            int index = this.ListGroups.IndexFromPoint(this.ListGroups.PointToClient(new Point(e.X, e.Y)));
            string group = this.ListGroups.Items[index].ToString();
            if (group.CompareTo(GROUP_ALL) == 0)
                return;

            string owner = (string)e.Data.GetData(DataFormats.StringFormat);
            Owners ownerclass = Owners.FindOwner(owner);
            if (ownerclass == null)
                return;

            if (ownerclass.Group == group)
                return;

            ownerclass.Group = group;
            this.ListChanged = true;
            this.ListGroups_SelectedIndexChanged(sender, e);
        }


        //text box for user name 
        private string savedUsername = "";
        private void textusername_TextChanged(object sender, EventArgs e)
        {
            ChangeButtonVisibility();
        }
        //text box for password 
        private string savedPassword = "";
        private void textpassword_TextChanged(object sender, EventArgs e)
        {
            ChangeButtonVisibility();
        }
        //text box for field
        private string savedFieldata = "";
        private void textfield_TextChanged(object sender, EventArgs e)
        {
            ChangeButtonVisibility();
        }
        //edit data button
        private void ButtonEdit_Click(object sender, EventArgs e)
        {
            Owners owner_class = Owners.FindOwner(this.m_currentOwner);

            if ( this.textusername.Text==owner_class.Username && this.textpassword.Text==owner_class.Password && this.textfield.Text==owner_class.Fielddata )
            {
                AppDialogWrapper.PromptBox("No data was changed","Attention");

                this.ButtonEdit.Visible = false;

                return;
            }

            if (AppDialogWrapper.PromptBox("Save the following data for " + this.ListOwners.Text + "? \nusername: " + this.textusername.Text + "\npassword: " + this.textpassword.Text + "\n" + this.label_field.Text + ": " + this.textfield.Text, "Confirm Change", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.ListChanged = true;
                this.labelSaved.Visible = false;

                owner_class.Username = this.textusername.Text;
                owner_class.Password = this.textpassword.Text;
                owner_class.Fielddata = this.textfield.Text;

                this.savedUsername = this.textusername.Text;
                this.savedPassword = this.textpassword.Text;
                this.savedFieldata = this.textfield.Text;

                this.ButtonEdit.Visible = false;
            }
        }
        //Control 'Change' button visibility
        private void ChangeButtonVisibility()
        {
            if (this.savedUsername.CompareTo(this.textusername.Text) != 0 ||
                this.savedPassword.CompareTo(this.textpassword.Text) != 0 ||
                this.savedFieldata.CompareTo(this.textfield.Text) != 0)
                    this.ButtonEdit.Visible = true;
            else
                    this.ButtonEdit.Visible = false;
        }

        //search box changed
        private void TextSearch_TextChanged(object sender, EventArgs e)
        {
            string check;
            string text = this.TextSearch.Text;
            text = text.ToLower();

            if (text.Length == 0)
            {
                this.ListGroups_SelectedIndexChanged(sender, e);
                return;
            }

            this.ListGroups.SelectedIndex = 0;

            this.ListOwners.Items.Clear();

            List<Owners> list = Owners.GetOwners();

            if (this.CheckBoxContain.Checked)
            {
                foreach (Owners temp in list)
                {
                    check = temp.Owner.ToLower();
                    if (check.Contains(text))
                        this.ListOwners.Items.Add(temp.Owner);
                }
            }
            else
            {
                foreach (Owners temp in list)
                {
                    check = temp.Owner.ToLower();
                    if (check.StartsWith(text))
                        this.ListOwners.Items.Add(temp.Owner);
                }
            }

            if (ListOwners.Items.Count > 0)
                this.ListOwners.SelectedIndex = 0;
        }
        //contain checkbox changed
        private void CheckBoxContain_CheckedChanged(object sender, EventArgs e)
        {
            this.DefaultChanged = true;

            this.TextSearch_TextChanged(sender, e);
        }
        //display checkbox changed
        private void CheckBoxDisplay_CheckedChanged(object sender, EventArgs e)
        {
            this.DefaultChanged = true;

            this.ListOwners_SelectedIndexChanged(sender, e);
        }


        //load profile data ( load click )
        private void LoadButton_Click(object sender, EventArgs e)
        {
            this.MenuLoad_Click(sender, e);
        }
        //save profile data ( save click )
        private void SaveButton_Click(object sender, EventArgs e)
        {
            this.MenuSave_Click(sender, e);
        }
        //get the path for the profile
        public static string GetProfilePath(string fileName, bool isOpened)
        {
            string extention = GetProfileExtention(isOpened);
            return string.Format(@"{0}\{1}{2}", PROFILES_PATH, fileName, extention);
        }
        //get the current profile extention - if opened, change to SOD_OPEN, else SOD.
        private static string GetProfileExtention(bool isOpened)
        {
            //return (isOpened) ? PASSWORD_EXTENTION_OPEN : PASSWORD_EXTENTION;
            return PASSWORD_EXTENTION;
        }
        //adjust profile extention
        public static void SetProfileExtention(string profileName, bool isOpened)
        {
            File.Move(GetProfilePath(profileName, !isOpened), GetProfilePath(profileName, isOpened));
        }

        //
        //menu options
        //
        //menu file options
        public void MenuNew_Click(object sender, EventArgs e)
        {
            if (this.ListChanged)
            {
                switch (AppDialogWrapper.PromptBox("You have made changes since the last save, do you want to save now?", "Save Before New?", MessageBoxButtons.YesNoCancel))
                {
                    case DialogResult.Cancel:
                        return;
                    case DialogResult.Yes:
                        SaveProfile_Form saveform = new SaveProfile_Form(this);
                        saveform.SaveMode = SaveProfile_Form.SaveProfileMode.SaveAndNew;
                        saveform.Show();
                        return;
                    case DialogResult.No:
                        break;
                }
            }

            this.ListChanged = false;
            this.labelSaved.Visible = false;

            this.ProfileLabel = DEFAULT_PROFILE_NAME;
            this.CurrentPassword = "";

            Owners.ClearList();

            this.InitLists();

            this.tabControl.SelectedIndex = 0;

            this.CheckBoxContain.Checked = true;
            this.CheckBoxDisplay.Checked = true;
        }
        private void MenuLoad_Click(object sender, EventArgs e)
        {
            if (LoadProfile_Form.CheckSavedProfiles() == false)
            {
                AppDialogWrapper.PromptBox("There are no saved profiles","Attention");
                return;
            }

            if (this.ListChanged)
            {
                switch (AppDialogWrapper.PromptBox("You have made changes since the last save, do you want to save now?", "Save Before Load?", MessageBoxButtons.YesNoCancel))
                {
                    case DialogResult.Cancel:
                        return;
                    case DialogResult.Yes:
                        SaveProfile_Form saveform = new SaveProfile_Form(this);
                        saveform.SaveMode = SaveProfile_Form.SaveProfileMode.SaveAndLoad;
                        saveform.ShowDialog();
                        return;
                    case DialogResult.No:
                        break;
                }
            }
            LoadProfile_Form loadform = new LoadProfile_Form(this);
            loadform.ShowDialog();

            this.tabControl.SelectedIndex = 0;
        }
        private void MenuSave_Click(object sender, EventArgs e)
        {
            if (ProfileLabel.CompareTo(DEFAULT_PROFILE_NAME) == 0)
            {
                MenuSaveAs_Click(sender, e);
                return;
            }

            SaveProfile_Form saveform = new SaveProfile_Form(this, CurrentPassword);
            saveform.SaveProfile(ProfileLabel);
        }
        private void MenuSaveAs_Click(object sender, EventArgs e)
        {
            SaveProfile_Form saveform = new SaveProfile_Form(this);
            saveform.ShowDialog();
        }
        private void MenuClose_Click(object sender, EventArgs e)
        {
            if (this.ListChanged)
            {
                switch (AppDialogWrapper.PromptBox("You have made changes since the last save, do you want to save now?", "Save Before Load?", MessageBoxButtons.YesNoCancel))
                {
                    case DialogResult.Cancel:
                        return;
                    case DialogResult.Yes:
                        SaveProfile_Form saveform = new SaveProfile_Form(this);
                        saveform.SaveMode = SaveProfile_Form.SaveProfileMode.SaveAndExit;
                        saveform.ShowDialog();
                        return;
                    case DialogResult.No:
                        break;
                }
            }

            // if i got here , it means there was no save before finish-click ->that means that i need to save the default profile data here. 
            //(otherwise, it is made in the saveform class)
            if (this.DefaultChanged && this.ProfileLabel.Length > 0)
            {
                ProfileData profile = ProfileData.FindProfileData(this.ProfileLabel);
                profile.SearchContain = this.CheckBoxContain.Checked;
                profile.DisplayData = this.CheckBoxDisplay.Checked;
                ProfileData.SaveProfileData();
            }

            //hide and dispose
            this.Hide();
            this.Dispose();
        }
        //Import-Export
        private void MenuProfileImport_Click(object sender, EventArgs e)
        {
            string profile = ProfileLabel;
            string source_path;

            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select source file";
            ofd.InitialDirectory = Environment.SpecialFolder.Desktop.ToString();
            if (ofd.ShowDialog() == DialogResult.Cancel)
                return;

            source_path = ofd.FileName;

            if (Path.GetExtension(source_path).CompareTo(PASSWORD_EXTENTION.Substring(1)) != 0)
            {
                AppDialogWrapper.PromptBox("File extension should be .SOD and be an output of PasswordControl program", "Wrong Input");
                return;
            }

            string password = "";
            if (AppDialogWrapper.InputBox("Insert Password", "Insert the imported file password:", ref password, true) == DialogResult.Cancel)
                return;

            List<Owners> data = LoadImported(source_path, password);

            if (data == null) return;

            if (Owners.Contains(data))
            {
                AppDialogWrapper.PromptBox("Profile to import contain names that already exist", "Import Error");
                return;
            }

            //foreach (Owners owner in data)
                //Owners.Add_Owner(owner);

            InitLists(data,false);
            ListChanged = true;
        }
        private void MenuProfileExport_Click(object sender, EventArgs e)
        {
            string profile = ProfileLabel;
            string source_path = string.Format(@"{0}\{1}{2}", PROFILES_PATH, profile, PASSWORD_EXTENTION);
            string target_path;

            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.Description = "Select Target Folder:";
            fb.RootFolder = Environment.SpecialFolder.Desktop;
            DialogResult dr = fb.ShowDialog();
            if (dr == DialogResult.Cancel) return;

            target_path = string.Format(@"{0}\{1}{2}", fb.SelectedPath, profile, PASSWORD_EXTENTION);

            if ( File.Exists(target_path)  && AppDialogWrapper.PromptBox("File already exists in the current path, do you want to overwrite it?","File Exists",MessageBoxButtons.YesNo) == DialogResult.No )
                return;

            File.Copy(source_path, target_path, true);
        }
        private void MenuExecExport_Click(object sender, EventArgs e)
        {
            ImportExport.Export(Owners.GetOwners());
        }
        private void MenuExecImport_Click(object sender, EventArgs e)
        {
            ImportExport.Import();
            this.ListGroups_SelectedIndexChanged(sender, e);
            this.ListChanged = true;
        }
        private void MenuCreateTemplate_Click(object sender, EventArgs e)
        {
            ImportExport.CreateTemplate();
        }
        
        //Edit options
        private void EditNewGroup_Click(object sender, EventArgs e)
        {
            string newGroup = "";
            if (AppDialogWrapper.InputBox("New Group", "Insert the new group's name:", ref newGroup, false) == DialogResult.Cancel)
                return;

            if (newGroup.Length == 0)
            {
                AppDialogWrapper.PromptBox("New group name can't be blank","Input Error");
                return;
            }

            if (this.ListGroups.Items.Contains(newGroup))
            {
                AppDialogWrapper.PromptBox("This group is not new","Attention");
                return;
            }

            this.combogroup.Items.Add(newGroup);
            this.ListGroups.Items.Insert(this.ListGroups.Items.Count - 1, newGroup);
        }
        private void EditDeleteGroup_Click(object sender, EventArgs e)
        {
            int index = this.ListGroups.SelectedIndex;
            string group = this.ListGroups.Items[index].ToString();

            this.ListGroups.SelectedIndex = 0;

            if (group == GROUP_DELETED)
            {
                if (AppDialogWrapper.PromptBox("You are about to clear the deleted group, you will lose the data forever", "Clear Deleted?", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    return;
                List<string> owners = new List<string>();
                foreach (Owners temp in Owners.GetOwners())
                {
                    if (temp.Group == GROUP_DELETED)
                        owners.Add(temp.Owner); 
                }
                foreach (string temp in owners)
                    Owners.RemoveOwner(temp);
                this.ListChanged = true;
                this.ListGroups_SelectedIndexChanged(sender, e);
                return;
            }

            if (group == GROUP_ALL || group == GROUP_GENERAL)
            {
                AppDialogWrapper.PromptBox("This group can't be deleted", "Can't Delete");
                return;
            }

            foreach (Owners temp in Owners.GetOwners())
            {
                if (temp.Group == group)
                {
                    if (AppDialogWrapper.PromptBox("This group is not empty, delete all the items in the group?", "Confirm group delete", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        return;
                    }
                    break;
                }
            }

            foreach (Owners temp in Owners.GetOwners())
            {
                if (temp.Group == group)
                    temp.Group = GROUP_DELETED;
            }
            this.ListGroups.Items.Remove(group);
            this.combogroup.Items.Remove(group);
            this.ListChanged = true;
            this.labelSaved.Visible = false;
            this.ListGroups_SelectedIndexChanged(sender, e);
        }
        private void EditDeleteOwner_Click(object sender, EventArgs e)
        {
            int index = this.ListOwners.SelectedIndex;
            string ownerName = this.ListOwners.Items[index].ToString();
            Owners owner = Owners.FindOwner(ownerName);

            if (owner.Group.CompareTo(GROUP_DELETED) == 0)
            {
                if (AppDialogWrapper.PromptBox("You are about to delete permanently: " + owner.Owner, "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    return;
                Owners.RemoveOwner(owner.Owner);
            }
            else
            {
                if (AppDialogWrapper.PromptBox("Move " + owner.Owner + " to Deleted group?", "Confirm Delete", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    return;
                owner.Group = GROUP_DELETED;
            }

            this.ListChanged = true;
            this.labelSaved.Visible = false;
            this.ListGroups_SelectedIndexChanged(sender, e);
        }

        //menu help options
        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AppDialogWrapper.PromptBox("Just Call 052-3512233 - Ask for Ohad :)","Call Me");
        }
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
        }


        //
        //Initiate
        //
        public void InitLists()
        {
            this.ListOwners.Items.Clear();
            this.ListGroups.Items.Clear();
            this.combogroup.Items.Clear();

            this.combogroup.Items.Add(GROUP_GENERAL); this.combogroup.SelectedItem = this.combogroup.Items[0]; 
            this.ListGroups.Items.Add(GROUP_ALL); this.ListGroups.Items.Add(GROUP_GENERAL); this.ListGroups.Items.Add(GROUP_DELETED);
            this.ListGroups.SelectedItem = this.ListGroups.Items[0];
        }
        public void InitLists(List<Owners> data , bool clear) //clear is true if the Owners dictionary should be emptied
        {
            Owners.AddOwner(data,clear);

            this.ListOwners.Items.Clear();
            this.ListGroups.Items.Clear();
            this.combogroup.Items.Clear();

            this.combogroup.Items.Add(GROUP_GENERAL); this.combogroup.SelectedItem = this.combogroup.Items[0]; 
            this.ListGroups.Items.Add(GROUP_ALL); this.ListGroups.Items.Add(GROUP_GENERAL); this.ListGroups.Items.Add(GROUP_DELETED);
            this.ListGroups.SelectedItem = this.ListGroups.Items[0];

            foreach (Owners temp in data)
            {
                if (temp.Group.Length == 0) temp.Group = GROUP_GENERAL;
                if (!this.combogroup.Items.Contains(temp.Group) && temp.Group != GROUP_DELETED)
                {
                    this.combogroup.Items.Add(temp.Group);
                    this.ListGroups.Items.Insert(this.ListGroups.Items.Count - 1, temp.Group);
                }
                if (temp.Group != GROUP_DELETED)
                    this.ListOwners.Items.Add(temp.Owner);
            }

            if (this.ListOwners.Items.Count > 0)
                this.ListGroups_SelectedIndexChanged(new object(),new EventArgs());
            
        }
        public void InitProfile(string new_profile)
        {
            ProfileData profile_data;

            //save the prev default settings
            if (this.DefaultChanged)
            {
                profile_data = ProfileData.FindProfileData(this.ProfileLabel);
                if (profile_data != null)
                {
                    profile_data.SearchContain = this.CheckBoxContain.Checked;
                    profile_data.DisplayData = this.CheckBoxDisplay.Checked;
                    ProfileData.SaveProfileData();
                }
            }

            //load the next default settings
            profile_data = ProfileData.FindProfileData(new_profile);
            if (profile_data != null)
            {
                this.CheckBoxContain.Checked = profile_data.SearchContain;
                this.CheckBoxDisplay.Checked = profile_data.DisplayData;
            }
        }

        // 
        // load (for import) profile
        //
        private List<Owners> LoadImported(string path , string password)
        {
            return DataSerialize.LoadEncrypted<List<Owners>>(path, password);
        }
       
        //
        // tool tips
        //
        private void MakeToolTips()
        {
            // Set up the ToolTip text for the Button and Checkbox.

            // Add password section
                toolTip.SetToolTip(this.combogroup, "This is the group that will contain the new password");
                toolTip.SetToolTip(this.addowner,"What the password is associated to. Example: hotmail\\Gmail etc.");
                toolTip.SetToolTip(this.addusername, "The username used with the new password. Example: Timuti_George@hotmail.com");
                toolTip.SetToolTip(this.addpassword, "The new password");
                toolTip.SetToolTip(this.addfieldname, "Name of an optional field. Example: Emloyee ID");
                toolTip.SetToolTip(this.addfielddata, "The data associated with the optional field. Example: WID1234");
        }
    }
}

